package com.bjsxt.controller.erp;

import com.bjsxt.aspectj.annotation.Log;
import com.bjsxt.aspectj.enums.BusinessType;
import com.bjsxt.controller.BaseController;
import com.bjsxt.dto.ProviderDto;
import com.bjsxt.service.ProviderService;
import com.bjsxt.utils.ShiroSecurityUtils;
import com.bjsxt.vo.AjaxResult;
import com.bjsxt.vo.DataGridView;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 供应商控制层
 */
@RestController
@RequestMapping("erp/provider")
public class ProviderController extends BaseController {

    @Reference  //使用dubbo的引用
    private ProviderService providerService;

    /**
     * 分页查询供应商
     *
     * @param producterDto
     * @return
     */
    @GetMapping("listProviderForPage")
    @HystrixCommand
    public AjaxResult listProviderForPage(ProviderDto producterDto) {
        DataGridView dataGridView = this.providerService.listProviderForPage(producterDto);
        return AjaxResult.success("查询成功", dataGridView.getData(), dataGridView.getTotal());
    }

    /**
     * 添加供应商
     *
     * @param providerDto
     * @return
     */
    @PostMapping("addProvider")
    @Log(title = "添加供应商", businessType = BusinessType.INSERT)
    @HystrixCommand
    public AjaxResult addProvider(@Validated ProviderDto providerDto) {
        providerDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.providerService.addProvider(providerDto));
    }

    /**
     * 修改供应商
     *
     * @param providerDto
     * @return
     */
    @PutMapping("updateProvider")
    @Log(title = "修改供应商", businessType = BusinessType.UPDATE)
    @HystrixCommand
    public AjaxResult updateProvider(@Validated ProviderDto providerDto) {
        providerDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.providerService.updateProvider(providerDto));
    }

    /**
     * 根据id查询一个供应商
     *
     * @param providerId
     * @return
     */
    @GetMapping("getProviderById/{providerId}")
    @HystrixCommand
    public AjaxResult getProviderById(@Validated @PathVariable @NotNull(message = "供应商providerId不能为空") Long providerId) {
        return AjaxResult.success(this.providerService.getOne(providerId));
    }

    /**
     * 删除供应商
     *
     * @param providerIds
     * @return
     */
    @DeleteMapping("deleteProviderByIds/{providerIds}")
    @Log(title = "删除供应商", businessType = BusinessType.DELETE)
    @HystrixCommand
    public AjaxResult deleteProviderByIds(@Validated @PathVariable @NotNull(message = "供应商providerIds不能为空") Long[] providerIds) {
        return AjaxResult.toAjax(this.providerService.deleteProviderByIds(providerIds));
    }

    /**
     * 查询全部可用的供应商
     *
     * @return
     */
    @GetMapping("selectAllProvider")
    @HystrixCommand
    public AjaxResult selectAllProvider() {
        return AjaxResult.success(this.providerService.selectAllProvider());
    }
}
